package com.dingreading.cloud.web.service.impl;

import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.web.entity.CooperPayOrder;
import com.dingreading.cloud.web.entity.table.CooperPayOrderTableDef;
import com.dingreading.cloud.web.mapper.CooperPayOrderMapper;
import com.dingreading.cloud.web.service.CooperPayOrderService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CooperPayOrderServiceImpl extends BaseServiceImpl<CooperPayOrderMapper, CooperPayOrder, CooperPayOrderTableDef> implements CooperPayOrderService {

    @Override
    protected CooperPayOrderTableDef getTable() {
        return CooperPayOrderTableDef.cooperPayOrder;
    }


    @Override
    public Integer getOrderStatus(String code) {
        QueryCondition condition = table.code.eq(code).or(table.orderUid.eq(code));
        QueryWrapper wrapper = QueryWrapper.create().select(table.status).from(table).where(condition);
        return getMapper().selectObjectByQueryAs(wrapper, Integer.class);
    }

    @Override
    public CooperPayOrder getByCode(String code) {
        return getMapper().selectOneByCondition(table.code.eq(code));
    }

    @Override
    public CooperPayOrder getByOrderUid(String orderUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderUid.eq(orderUid));

        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public void updateCodeAndPayChannelCode(String orderUid, String code, String payChannelCode) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderUid.eq(orderUid));

        UpdateChain.of(CooperPayOrder.class)
                .set(table.code, code)
                .set(table.payChannelCode, payChannelCode)
                .where(condition)
                .update();
    }
}
